package com.iris.live.services.report.sources;

import com.iris.live.services.data.repositories.InsuranceRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/3.
 */
@Component("ExportOtherInsuranceAccountInfoSource")
public class ExportOtherInsuranceAccountInfoSource implements IExcelReportSource {

    private static Logger logger = LoggerFactory.getLogger(ExportOtherInsuranceAccountInfoSource.class);

    @Autowired
    private InsuranceRepository insuranceRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商_他店保险业务台账模板", "经销商_他店保险业务台账模板", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        String permit = context.get("permit") == null ? null : context.get("permit").toString(),
                icPolicyNo = context.get("icPolicyNo") == null ? null : context.get("icPolicyNo").toString(),
                bcPolicyNo = context.get("bcPolicyNo") == null ? null : context.get("bcPolicyNo").toString(),
                icInsuranceCpy = context.get("icInsuranceCpy") == null ? null : context.get("icInsuranceCpy").toString(),
                bcInsuranceCpy = context.get("bcInsuranceCpy") == null ? null : context.get("bcInsuranceCpy").toString(),
                sale = context.get("sale") == null ? null : context.get("sale").toString(),
                cusName = context.get("cusName") == null ? null : context.get("cusName").toString(),
                carBrand = context.get("carBrand") == null ? null : context.get("carBrand").toString(),
                carModel = context.get("carModel") == null ? null : context.get("carModel").toString();
        int pageSize = Integer.parseInt(context.get("pageSize") == null ? "0" : context.get("pageSize").toString()),
                pageIndex = Integer.parseInt(context.get("pageIndex") == null ? "0" : context.get("pageIndex").toString());

        PageData pageData = insuranceRepository.exportOtherInsuranceAccountInfo(permit, icPolicyNo, bcPolicyNo, icInsuranceCpy, bcInsuranceCpy,
                sale, cusName, carBrand, carModel, pageSize, pageIndex);
        List<Row> list = new ArrayList<>();
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        if (pageData != null) {
            List values = pageData.getData();
            if (values != null) {
                Map<String, String> compareMap = new HashMap<>();
                for (Object o : values) {
                    int compareResult = 0;
                    Map<String, Object> map = (Map) o;
                    String tel = map.get("insTel") == null ? "" : map.get("insTel").toString();
                    if (compareMap.get(tel) != null && !"".equals(compareMap.get(tel))) {
                        compareResult = Integer.parseInt(compareMap.get(tel) == null ? "0" : compareMap.get(tel)) + 1;
                    }
                    compareMap.put(tel, compareResult + "");
                    list.add(new Row(getCells(map, compareMap)));
                }
            }
        }
        return list;
    }

    private Cell[] getCells(Map<String, Object> map, Map<String, String> compareMap) {
        Cell[] cells = new Cell[64];
        int index = 0;
        cells[index++] = new Cell(map.get("人员姓名") == null ? "" : map.get("人员姓名").toString(), index);
        cells[index++] = new Cell(map.get("year") == null ? "" : map.get("year").toString(), index);
        cells[index++] = new Cell(map.get("") == null ? "" : map.get("").toString(), index);
        cells[index++] = new Cell(map.get("") == null ? "" : map.get("").toString(), index);
        cells[index++] = new Cell(map.get("insName") == null ? "" : map.get("insName").toString(), index);
        String tel = map.get("insTel") == null ? "" : map.get("insTel").toString();
        cells[index++] = new Cell(tel, index);
        cells[index++] = new Cell(map.get("") == null ? "" : map.get("").toString(), index);
        cells[index++] = new Cell(map.get("model") == null ? "" : map.get("model").toString(), index);
        cells[index++] = new Cell(map.get("modelshort") == null ? "" : map.get("modelshort").toString(), index);
        cells[index++] = new Cell(map.get("VIN") == null ? "" : map.get("VIN").toString(), index);
        cells[index++] = new Cell(map.get("checkdate") == null ? "" : map.get("checkdate").toString(), index);
        //if (compareMap.get(tel) == null || "0".equals(compareMap.get(tel))) {
            cells[index++] = new Cell(map.get("forced_number") == null ? "" : map.get("forced_number").toString(), index);
            cells[index++] = new Cell(map.get("forced_ins_com") == null ? "" : map.get("forced_ins_com").toString(), index);
            cells[index++] = new Cell(map.get("交强险保费") == null ? "" : map.get("交强险保费").toString(), index);
            cells[index++] = new Cell(map.get("forced_op_time") == null ? "" : map.get("forced_op_time").toString(), index);
            cells[index++] = new Cell(map.get("forced_poundage") == null ? "" : map.get("forced_poundage").toString(), index);
            cells[index++] = new Cell(map.get("forced_insure_start") == null ? "" : map.get("forced_insure_start").toString(), index);
            cells[index++] = new Cell(map.get("forced_insure_end") == null ? "" : map.get("forced_insure_end").toString(), index);
            cells[index++] = new Cell(map.get("车船税") == null ? "" : map.get("车船税").toString(), index);
//        } else {
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//            cells[index++] = new Cell("", index);
//        }
        cells[index++] = new Cell(map.get("bus_number") == null ? "" : map.get("bus_number").toString(), index);
        cells[index++] = new Cell(map.get("bus_ins_com") == null ? "" : map.get("bus_ins_com").toString(), index);
        cells[index++] = new Cell(map.get("商业险保费") == null ? "" : map.get("商业险保费").toString(), index);
        cells[index++] = new Cell(map.get("bus_op_time") == null ? "" : map.get("bus_op_time").toString(), index);
        cells[index++] = new Cell(map.get("bus_poundage") == null ? "" : map.get("bus_poundage").toString(), index);
        cells[index++] = new Cell(map.get("bus_insure_start") == null ? "" : map.get("bus_insure_start").toString(), index);
        cells[index++] = new Cell(map.get("bus_insure_end") == null ? "" : map.get("bus_insure_end").toString(), index);
        cells[index++] = new Cell(map.get("endorsement_type") == null ? "" : map.get("endorsement_type").toString(), index);
        cells[index++] = new Cell(map.get("车辆损失险/保额") == null ? "" : map.get("车辆损失险/保额").toString(), index);
        cells[index++] = new Cell(map.get("车辆损失险/保费") == null ? "" : map.get("车辆损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车辆损失险/不计免赔") == null ? "" : map.get("车辆损失险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("商业三者责任险/保额") == null ? "" : map.get("商业三者责任险/保额").toString(), index);
        cells[index++] = new Cell(map.get("商业三者责任险/保费") == null ? "" : map.get("商业三者责任险/保费").toString(), index);
        cells[index++] = new Cell(map.get("商业三者责任险/不计免赔") == null ? "" : map.get("商业三者责任险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("全车盗抢险/保费") == null ? "" : map.get("全车盗抢险/保费").toString(), index);
        cells[index++] = new Cell(map.get("全车盗抢险/不计免赔") == null ? "" : map.get("全车盗抢险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-司机/保额") == null ? "" : map.get("车上人员责任险-司机/保额").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-司机/保费") == null ? "" : map.get("车上人员责任险-司机/保费").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-司机/不计免赔") == null ? "" : map.get("车上人员责任险-司机/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-乘客/保额") == null ? "" : map.get("车上人员责任险-乘客/保额").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-乘客/保费") == null ? "" : map.get("车上人员责任险-乘客/保费").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-乘客/不计免赔") == null ? "" : map.get("车上人员责任险-乘客/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("玻璃单独破碎险/保额") == null ? "" : map.get("玻璃单独破碎险/保额").toString(), index);
        cells[index++] = new Cell(map.get("玻璃单独破碎险/保费") == null ? "" : map.get("玻璃单独破碎险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车身划痕损失险/保额") == null ? "" : map.get("车身划痕损失险/保额").toString(), index);
        cells[index++] = new Cell(map.get("车身划痕损失险/保费") == null ? "" : map.get("车身划痕损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车身划痕损失险/不计免赔") == null ? "" : map.get("车身划痕损失险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("自燃损失险/保费") == null ? "" : map.get("自燃损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("发动机涉水损失险/保费") == null ? "" : map.get("发动机涉水损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("倒车镜、车灯单独损失险/保费") == null ? "" : map.get("倒车镜、车灯单独损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("新增设备损失险/保额") == null ? "" : map.get("新增设备损失险/保额").toString(), index);
        cells[index++] = new Cell(map.get("新增设备损失险/保费") == null ? "" : map.get("新增设备损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("指定修理厂险/保费") == null ? "" : map.get("指定修理厂险/保费").toString(), index);
        cells[index++] = new Cell(map.get("修理期间费用补偿险/保额") == null ? "" : map.get("修理期间费用补偿险/保额").toString(), index);
        cells[index++] = new Cell(map.get("修理期间费用补偿险/保费") == null ? "" : map.get("修理期间费用补偿险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车上货物责任险/保额") == null ? "" : map.get("车上货物责任险/保额").toString(), index);
        cells[index++] = new Cell(map.get("车上货物责任险/保费") == null ? "" : map.get("车上货物责任险/保费").toString(), index);
        cells[index++] = new Cell(map.get("精神损害抚慰金责任险/保额") == null ? "" : map.get("精神损害抚慰金责任险/保额").toString(), index);
        cells[index++] = new Cell(map.get("精神损害抚慰金责任险/保费") == null ? "" : map.get("精神损害抚慰金责任险/保费").toString(), index);
        cells[index++] = new Cell(map.get("机动车损失保险无法找到第三方特约险/保费") == null ? "" : map.get("机动车损失保险无法找到第三方特约险/保费").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种一/保额") == null ? "" : map.get("自定义险种一/保额").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种一/保费") == null ? "" : map.get("自定义险种一/保费").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种二/保额") == null ? "" : map.get("自定义险种二/保额").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种二/保费") == null ? "" : map.get("自定义险种二/保费").toString(), index);
        cells[index++] = new Cell(map.get("附加险/不计免赔") == null ? "" : map.get("附加险/不计免赔").toString(), index);
        return cells;
    }
}
